classdef PreHelper< handle
    properties (Constant)
        
    end
    methods(Static = true)
        function data = DoOnePreProcessing(x_data, type, bShow)
            switch type
                case 1
                    data = PreHelper.DoSG(x_data);
                case 2
                    data = PreHelper.DoMSC(x_data);
                case 3
                    data = PreHelper.DoSNV(x_data);
                case 4
                    data = PreHelper.DoCR(x_data, bShow);
                case 5
                    data = PreHelper.DoMA(x_data, 3);%
                    data = PreHelper.DoDiff(data, 1, bShow);
                otherwise
                    disp('error in DoOnePreProcessing');
            end
        end
        
        function XX = DoDiff(XX, n, bShow)
            
            if n == 1
                XX = diff(XX); 
            else
                XX = diff(XX, n); 
            end
            
            if bShow 
               plot(XX); 
            end
        end
        
        
        function XX = DoMA(XX, windowSize)
            type = 'simple';
%             windowSize = 7;
            
             for i = 1 : size(XX, 2)      
                x = XX(:, i);
                ma = movavg(x, type, windowSize);
                XX(:, i) = ma;
% 
%                 
%                 plot(x,':')
%                 hold on
%                 plot(ma,'.-')
%                 legend('signal','ma')
            end
        end
        %% size(XX) = 204*24
        function XX = DoCR(XX, bShow)
            bands = 1 : size(XX, 1);
            bands = bands;
            for i = 1 : size(XX, 2)%%length(XX)
                x = XX(:, i);
                
%                 %%可行，                
% %                 [result_x, result_y] = Envelope(bands, x);%调用自定义的提取包络线函数
% %                 y = interp1(result_x, result_y, bands, 'linear')';                
% %                 XX(:, i) = x./y;                

                %% 2024-02-07改这个
                cr = continuum_removed(x', 'Sampling', bands, 'Plots','no');
                XX(:, i) = cr';                

                if bShow 
                   figure
                   hold on
                   plot(x, 'r-')
                   plot(XX(:, i), 'b.')
                   hold off
                end
            end
        end
        
        function XX = DoSG(XX, order, framelen)
            if nargin < 3
                order = 3;%%2023-1-2 改 4
                framelen = 11;%%17
            end
            for i = 1 : size(XX, 2)%%length(XX)
                
%                 x = XX{i}(:, 4);
                x = XX(:, i);
                sgf = sgolayfilt(x,order,framelen);
                XX(:, i) = sgf;
% % %                 XX{i}(:, 4) = sgf;
                
                % %                 plot(x,':')
                % %                 hold on
                % %                 plot(sgf,'.-')
                % %                 legend('signal','sgolay')
            end
            
        end
        %%XX, 204*24
        function XX = DoSNV(XX)
     
            x_avg = mean(XX);
            x_std = std(XX);
            
            XX = (XX - x_avg) ./ x_std;
         
% %             x_avg = mean(XX,2);
% %             x_std = std(XX,0,2);
% %             
% %             XX = (XX - x_avg) ./ x_std;
            
% %             figure  %下面代码用作画图
% %             for i = 1 : size(XX, 2)%%length(XX)               
% %                 y = XX(:, i);  
% %                 hold on
% %                 plot(y,'.-')
% %                 legend('signal','msc')
% %             end
        end
        
        
        %%MSC预处理 XX，204*24
        function XX = DoMSC(XX, x_ref)            
            if(nargin == 1)
                x_ref = mean(XX, 2);
            end            
% %                         figure,hold on   
            for i = 1 : size(XX, 2) %%length(XX)
                x = XX(:, i);
% % %                 x_msc = PreHelper.my_msc(x, x_sta);

                pf = polyfit(x_ref, x, 1);
                x_msc  = (x - pf(2)) / pf(1);   
                XX(:, i) = x_msc;
                
% %                                 plot(x, ':')
% %                                 hold on
% %                                 plot(x_msc,'.-')
% %                                 legend('signal','msc')
            end
            
% %             hold off
        end

        


    end
end